package com.itcast.donkey.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.modles.DetailedTravel_area_Modles;

import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/9/10.
 */
public class GridViewImageHotleAdapter extends BaseAdapter{

    private Context context;
    private List<DetailedTravel_area_Modles.HotelsBean> hotelsBeen = new ArrayList<>();
    ViewHolder holder = null;

    public GridViewImageHotleAdapter(Context context, List<DetailedTravel_area_Modles.HotelsBean> hotelsBeen) {
        this.context = context;
        this.hotelsBeen = hotelsBeen;
    }

    @Override
    public int getCount() {
        return hotelsBeen.size();
    }

    @Override
    public Object getItem(int position) {
        return hotelsBeen.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        holder = new ViewHolder();
        if (convertView == null) {

            convertView = View.inflate(context, R.layout.item_grid_image_tuijian, null);
            holder.iv = (ImageView) convertView.findViewById(R.id.iv_image);
            holder.tv = (TextView) convertView.findViewById(R.id.tv_grid);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv.setText(hotelsBeen.get(position).getName_zh_cn());
        ImageOptions imageOptions = new ImageOptions.Builder()
                .setSize(DensityUtil.dip2px(120), DensityUtil.dip2px(120))//图片大小
                .setRadius(DensityUtil.dip2px(5))//ImageView圆角半径
                .setCrop(true)// 如果ImageView的大小不是定义为wrap_content, 不要crop.
                .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                .setLoadingDrawableId(R.mipmap.ic_launcher)//加载中默认显示图片
                .setFailureDrawableId(R.mipmap.ic_launcher)//加载失败后默认显示图片
                .setUseMemCache(true)//缓存
                .setIgnoreGif(false)//不忽略动态图
                .build();
        x.image().bind(holder.iv, hotelsBeen.get(position).getImage_url(), imageOptions);

        return convertView;
    }

    public class ViewHolder {
        ImageView iv;
        TextView tv;
    }
}
